package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjreply;
import com.chezhujie.www.enums.Approvestatus;
import com.chezhujie.www.service.CzjreplyService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   回复评论
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjreply")
public class CzjreplyController extends BaseController<Czjreply> {
    @Autowired
    private CzjreplyService czjreplyService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjreply czjreply, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjreply,req,model);
        setUserinfoId(model);
        setCommentsId(model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjreply);
            Pagination pagination = czjreplyService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取回复评论列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjreply);
        return "czjreply/list";
    }
    /**
    * api接口回复评论列表
    * @param pageNo
    * @param pageSize
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, Czjreply czjreply,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        try {
            czjreply.setApproveStatus(Approvestatus.SUCCESS.getIndex());
//            czjreply.setDelFlag(0);
            Map<String, Object> params = ConvertObj.toMap(czjreply);
            Pagination pagination = czjreplyService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取回复评论列表失败!"+e,"",resp);
        }

    }
    /**
    * api接口回复评论详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = czjreplyService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取回复评论详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "czjreply/apiTest";

    }

    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjreply czjreply,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjreply.getReplyId())){
                czjreplyService.update(czjreply);


            }else{
                czjreply.setReplyId(UUIDUtils.getUUID());
                czjreplyService.insert(czjreply);
            }
            model.addAttribute("success", "保存回复评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存回复评论失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String replyId,Czjreply czjreply, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjreply,req,model);
        setUserinfoId(model);
        setCommentsId(model);
        try {
            Czjreply entity = czjreplyService.queryById(czjreply.getReplyId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询回复评论出错!详情："+e.getMessage());
        }
        return "czjreply/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjreply czjreply, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjreply,req,model);
        setUserinfoId(model);
        setCommentsId(model);
        if(StringUtils.isNotBlank(czjreply.getReplyId())){
            czjreply = czjreplyService.queryById(czjreply.getReplyId());
            model.addAttribute("entity", czjreply);
        }
        return "czjreply/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String replyId,Czjreply czjreply, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjreplyService.deleteById(czjreply.getReplyId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setUserinfoId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listUserinfoId = czjreplyService.getListUserinfoId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listUserinfoId", listUserinfoId);
    }
    private void setCommentsId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listCommentsId = czjreplyService.getListCommentsId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listCommentsId", listCommentsId);
    }
    /** 审批 **/
    @RequestMapping("/approve.do")
    public String approve(@RequestParam(required = true)String replyId,
                          @RequestParam(required = true)Integer approveStatus,
                          HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model){

        czjreplyService.changeApprove(replyId,approveStatus);
        return "redirect:detail.do?replyId="+replyId;
    }

    private void setMsg(Czjreply czjreply,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjreply);
    }
}
